/*
    Copyright  Louis CHEREL cherel.louis@gmail.com  
    This file is part of Agenda de l'Efrei.

    Agenda de l'Efrei is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Agenda de l'Efrei is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Agenda de l'Efrei.  If not, see <http://www.gnu.org/licenses/>.
 */

package fr.efrei.AgendaEfrei;

import android.app.ActionBar;
import android.app.ActionBar.OnNavigationListener;
import android.content.Context;
import android.os.AsyncTask;
import android.util.Log;
import android.util.Xml;
import android.widget.ArrayAdapter;
import java.io.FileReader;
import org.xmlpull.v1.XmlPullParser;

public class ReadWeeks extends AsyncTask<ActionBar,Void,Boolean>{
            

    protected Long mLastUpdate;
    protected String[] mWeekNames = new String[5];
    protected String[] mWeekIndices = new String[5];
    //protected int mCurrentWeek;
    protected ActionBar mActionbar;
    protected Context mParent;
            
        ReadWeeks(Long last,String[] names,String[] indices,Context parent){
            mLastUpdate = last;
            mWeekNames = names;
            mWeekIndices = indices;
            //mCurrentWeek= currentWeek;
            mParent = parent;
        }

        @Override
        protected Boolean doInBackground(ActionBar...ac) {
            /* la structure est:
             * <weeks>
             *      <update>timestamp</update>
             *      <week>
             *          <number></number>
             *          <name></name>
             *      </week>
             * </weeks>
             */
            Log.v("ReadWeeks","started");
            mActionbar = ac[0];
            mWeekNames[0]="";
            boolean iW=true;
            while(iW){
                iW=MainActivity.getIsWeeksUpdaterLaunched();
            }
            
            FileReader file=null;
            int i=-1;
            try{
                XmlPullParser parser = Xml.newPullParser();
                parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES,false);

                file = new FileReader(mParent.getFilesDir()+"weeks");
                parser.setInput(file);
                parser.nextTag();
                parser.require(XmlPullParser.START_TAG, null, "weeks");


                while(parser.next() != XmlPullParser.END_DOCUMENT){

                    if (parser.getEventType() != XmlPullParser.START_TAG){
                        continue;
                    }

                    String name = parser.getName();


                    if(name.equals("update")) {

                        if(parser.next() == XmlPullParser.TEXT){

                            mLastUpdate=Long.parseLong(parser.getText());

                        }

                    }
                    else if(name.equals("week")){
                        i+=1;
                    }
                    else if(name.equals("number"))
                    {

                        if(parser.next() == XmlPullParser.TEXT){
                            mWeekIndices[i] = parser.getText();
                        }

                    }
                    else if(name.equals("name"))
                    {
                        if(parser.next() == XmlPullParser.TEXT){
                            mWeekNames[i] = parser.getText();
                        }
                    }

                }
                file.close();
                return true;
            } catch (Exception e){
                try{
                    file.close();
                } catch(Exception e2){}
                return false;
            }

            
        }
        
        @Override
        protected void onPostExecute(Boolean b) {
            if(!b)
            {
                mWeekNames[0]="Please";
                mWeekNames[1]="Enable";
                mWeekNames[2]="Your";
                mWeekNames[3]="Internet";
                mWeekNames[4]="Connection";
            }
            ArrayAdapter<CharSequence> adapter = new ArrayAdapter(mParent, 
                                        R.layout.spinner,
                                        mWeekNames);
            mActionbar.setListNavigationCallbacks(adapter,(OnNavigationListener)mParent);
        }
    }
